﻿using System;
using System.Windows.Forms;
using Cesave.Presentation.Permisos.WinForms.Client.Views.Access;
using Cesave.Presentation.Permisos.WinForms.Client.Views.Main;
using Cesave.Application.Permisos.Settings;
using Cesave.Infrastructure.CrossCutting.IoC;

namespace Cesave.Presentation.Permisos.WinForms.Client
{
    public class PermisosWinFormsClientApplicationContext
        : ApplicationContext
    {

        #region Private properties

        /// <summary>
        /// Referencia a la vista de acceso.
        /// </summary>
        private LoginView _loginView;

        /// <summary>
        /// Referencia a la vista principal
        /// </summary>
        private MainView _mainView;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor para indicar las vistas de acceso y principal.
        /// </summary>
        /// <param name="loginView">
        /// Vista de acceso.
        /// </param>
        /// <param name="mainView">
        /// Vista principal
        /// </param>
        public PermisosWinFormsClientApplicationContext(LoginView loginView, MainView mainView)
            : base(loginView)
        {
            _loginView = loginView;
            _mainView = mainView;
        }

        #endregion

        #region ApplicationContext overrides

        /// <summary>
        /// <see cref="System.Windows.Forms.ApplicationContext"/>
        /// </summary>
        /// <param name="sender">
        /// <see cref="System.Windows.Forms.ApplicationContext"/>
        /// </param>
        /// <param name="e">
        /// <see cref="System.Windows.Forms.ApplicationContext"/>
        /// </param>
        protected override void OnMainFormClosed(object sender, EventArgs e)
        {
            if (sender is LoginView)
            {
                IPUSSettings pusSettings = IoCFactory.Resolve<IPUSSettings>();
                if (pusSettings.Agente != null)
                {
                    base.MainForm = _mainView;
                    base.MainForm.Show();
                }
                else
                {
                    System.Windows.Forms.Application.Exit();
                    return;
                }
            }
            else
                base.OnMainFormClosed(sender, e);
        }

        #endregion

    }
}
